package recruiterSession;

import java.rmi.RemoteException;
import java.util.Collection;

import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import company.Company;
import compsup.CompanySup;

import offer.Offer;
import recruiter.Recruiter;
import recruiter.RecruiterHome;
import student.Student;
import student.StudentHome;
import degree.Degree;
import degree.DegreeHome;
import descriptor.CompanySupsListDescriptor;
import descriptor.DegreeListDescriptor;
import descriptor.OfferDescriptor;
import descriptor.OfferListDescriptor;
import descriptor.RecruiterDescriptor;
import descriptor.StudentListDescriptor;

public class RecruiterSessionBean implements SessionBean{

public Recruiter recruiter;
public InitialContext initialContext;
public RecruiterHome recruiterHome;
	
	public int getId() throws RemoteException{
		return recruiter.getId();
	}
	public void setId(int id) throws RemoteException{
		recruiter.setId(id);
	}
	
	public String getLogin() throws RemoteException{
		return recruiter.getLogin();
	}
	public void setLogin(String login) throws RemoteException{
		recruiter.setLogin(login);
	}
	
	public String getPassword() throws RemoteException{
		return recruiter.getPassword();
	}
	public void setPassword(String password) throws RemoteException{
		recruiter.setPassword(password);
	}
	
	public String getFirstname() throws RemoteException{
		return recruiter.getFirstname();
	}
	public void setFirstname(String firstname) throws RemoteException{
		recruiter.setFirstname(firstname);
	}
	
	public String getSurname() throws RemoteException{
		return recruiter.getSurname();
	}
	public void setSurname(String surname) throws RemoteException{
		recruiter.setSurname(surname);
	}
	
	public String getEmail() throws RemoteException{
		return recruiter.getEmail();
	}
	public void setEmail(String email) throws RemoteException{
		recruiter.setEmail(email);
	}
	public Company getCompany() {
		return recruiter.getCompany();
	}
	public void setCompany(Company company) {
		recruiter.setCompany(company);
	}
	public RecruiterDescriptor getDescriptor(){
		RecruiterDescriptor desc = new RecruiterDescriptor(recruiter.getId(),recruiter.getFirstname(),recruiter.getSurname(),recruiter.getLogin(),recruiter.getPassword(),recruiter.getEmail(),recruiter.getCompany().getId(),new int[0]);
		return desc;
	}
	
	public CompanySupsListDescriptor getCompanySupForTheCompanyDescriptor(){
		
		Collection<CompanySup> compSupList = recruiter.getCompany().getCompanySups();
		
		CompanySupsListDescriptor desc = new CompanySupsListDescriptor();
		for(CompanySup compSup: compSupList){
			desc.addCompanySupDescriptor(compSup.toCompanySupDescriptor());
		}
		return desc;
	}
	
	public Recruiter getBean(){
		return recruiter;
	}
	
	public StudentListDescriptor getInterestedStudent(){
		StudentHome studentHome = null;
		Collection<Student> coll = null;
		StudentListDescriptor desc = new StudentListDescriptor();
		
		try {
			studentHome = (StudentHome)initialContext.lookup("MyStudent_L");
			coll = studentHome.findAllStudents();
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (FinderException e) {
			e.printStackTrace();
		}
		for(Student s :coll){
			if(s.getCompaniesPref().contains(recruiter.getCompany())){
				desc.addStudent(s.getDescriptor());
			}
			
		}
		return desc;
	}
	
	public DegreeListDescriptor getDegreeDescriptor(){
		DegreeListDescriptor desc = new DegreeListDescriptor();
		Collection<Degree> coll= null;
		DegreeHome degreeHome = null;
		
		try {
			degreeHome = (DegreeHome)initialContext.lookup("MyDegree_L");
			coll = degreeHome.findAllDegrees();
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (FinderException e) {
			e.printStackTrace();
		}
		for(Degree d: coll){
			desc.addDegree(d.getDescriptor());
		}
		return desc;
	}

	public OfferListDescriptor getOfferDescriptor(){
		OfferListDescriptor desc = new OfferListDescriptor();
		Collection<Offer> coll = recruiter.getOffers();
		for(Offer offer : coll){
			desc.addOffer(offer.getDescriptor());
		}
		
		return desc;
	}

	public String ejbCreate(int id){
		
		try {
			initialContext = new InitialContext();
			recruiterHome = (RecruiterHome)initialContext.lookup("MyRecruiter_L");
			recruiter = recruiterHome.findByPrimaryKey(id);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (FinderException e) {
			e.printStackTrace();
		}
		return null;
	}
	public void ejbPostCreate(int id){
		
	}
	public void ejbActivate() throws EJBException, RemoteException {
		
		
	}

	public void ejbPassivate() throws EJBException, RemoteException {
		
		
	}

	public void ejbRemove() throws EJBException, RemoteException {
		
		
	}

	public void setSessionContext(SessionContext arg0) throws EJBException, RemoteException {
		
		
	}

}
